<?php
class RequirementsController extends AppController {

	var $name = 'Requirements';
	var $helpers = array('Html','Form','Ajax','Javascript');
	
	var $paginate = array(
	'limit' => 5
	);

	function index() {
		$this->Requirement->recursive = 0;
		$this->set('requirements', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Requirement.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('requirement', $this->Requirement->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Requirement->create();
			if ($this->Requirement->save($this->data)) {
				$this->Session->setFlash(__('The Requirement has been saved', true));
				$this->redirect(array('controller'=>'projects','action'=>'user_projects'));
			} else {
				$this->Session->setFlash(__('The Requirement could not be saved. Please, try again.', true));
			}
		}
		$designStrategies = $this->Requirement->DesignStrategy->find('list');
		$projects = $this->Requirement->Project->find('list');
		$requirementTypes = $this->Requirement->RequirementType->find('list');
		$sources = $this->Requirement->Source->find('list');
		$users = $this->Requirement->User->find('list');
		$this->set(compact('designStrategies', 'projects', 'requirementTypes', 'sources', 'users'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Requirement', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Requirement->save($this->data)) {
				$this->Session->setFlash(__('The Requirement has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Requirement could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Requirement->read(null, $id);
		}
		$designStrategies = $this->Requirement->DesignStrategy->find('list');
		$projects = $this->Requirement->Project->find('list');
		$requirementTypes = $this->Requirement->RequirementType->find('list');
		$sources = $this->Requirement->Source->find('list');
		$users = $this->Requirement->User->find('list');
		$this->set(compact('designStrategies','projects','requirementTypes','sources','users'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Requirement', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Requirement->del($id)) {
			$this->Session->setFlash(__('Requirement deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}
	
	
	function list_requirements($requirement_type_id, $limit = null) {
		
		$this->layout = 'ajax';
		
		if($limit!= null)
		{
			$this->paginate['limit']=5;
			$this->set('css','include');
		}

		$this->Requirement->recursive = 0;
		$data = $this->paginate('Requirement', array('Requirement.requirement_type_id LIKE' => ''.$requirement_type_id, 'Requirement.project_id LIKE' => ''.$_SESSION['project_id']));
		$this->set('requirement_type_id',$requirement_type_id);
		$this->set('requirements', $data);
	}

	function new_requirement() {

		$this->layout = 'ajax';

		if (!empty($this->data)) {
			$this->Requirement->create();
			if ($this->Requirement->save($this->data)) {
				$this->Session->setFlash(__('The Requirement has been saved', true));
				$this->redirect(array('controller'=>'pages','action'=>'blank'));
			} else {
				$this->Session->setFlash(__('The Requirement could not be saved. Please, try again.', true));
			}
		}
		$designStrategies = $this->Requirement->DesignStrategy->find('list');
		$projects = $this->Requirement->Project->find('list');
		$requirementTypes = $this->Requirement->RequirementType->find('list');
		$sources = $this->Requirement->Source->find('list');
		$users = $this->Requirement->User->find('list');
		$this->set(compact('designStrategies', 'projects', 'requirementTypes', 'sources', 'users'));
	}
	

}
?>